package org.eoldroid.lostcall;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
//import android.content.ServiceConnection;
import android.database.Cursor;
import android.provider.Contacts.Phones;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
//import android.widget.Toast;

public class MainActivity extends ListActivity {
    /** Called when the activity is first created. */
	private ListAdapter adaptador;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor c = this.getContentResolver().query(People.CONTENT_URI, null, People.NAME + " IS NOT NULL", null, People.NAME + " ASC");
        this.startManagingCursor(c);
        
        String[] columnas = new String[] {People.NAME};
        int nombres[] = new int[] {R.id.nombre};
        
        adaptador = new SimpleCursorAdapter(this, R.layout.main, c, columnas, nombres);
        setListAdapter(adaptador);
        getListView().setTextFilterEnabled(true);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
    	super.onListItemClick(l, v, position, id);
    	Intent i = new Intent(Intent.ACTION_CALL);
    	
    	Cursor c = (Cursor) adaptador.getItem(position);
    	long idTelefono = c.getLong(c.getColumnIndex(People.PRIMARY_PHONE_ID));
    	i.setData(ContentUris.withAppendedId(Phones.CONTENT_URI, idTelefono));
    	//this.setTitle("Lost Call Intent");
    	
    	//Caller call = new Caller();
    	//call.bindService(i, null, 0);
    	//startService(i);
    	//startActivityForResult(i, 234);
        //Toast.makeText(this, "LLamando...",
        //        Toast.LENGTH_SHORT).show();
    	//try {
		//	Thread.sleep(10000);
		//} catch (Exception e) {
		//	e.printStackTrace();
		//}
		//stopService(i);
    	startActivity(i);
		finish();
    	//call.stopService(i);
    }
    @Override
    protected void onTitleChanged(CharSequence title, int color) {
    	// TODO Auto-generated method stub
    	//super.onTitleChanged(title, color);
    	/*if(title.equals("Lost Call Intent")){
	    	try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.finish();
    	}else{
    		super.onTitleChanged(title, color);
    	}*/
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	super.onActivityResult(requestCode, resultCode, data);
    	
    }
}